/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.cost;

import java.util.ArrayList;

import org.openaecfm.model.cost.AppliedValue;
import org.openaecfm.model.cost.ArithmeticOperatorEnum;
import org.openaecfm.model.datetime.Date;
import org.openaecfm.model.lang.AbstractEntity;
import org.openaecfm.model.measure.MeasureWithUnit;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.io.measure.ValueIO;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class AppliedValueIO extends
		AbstractEntityInstanceInputImpl<AppliedValue> implements
		IEntityInstanceOutput<AppliedValue>,
		IEntityInstanceUtilInput<AppliedValue>,
		IEntityInstanceUtilOutput<AppliedValue> {

	private ValueIO valueIO;

	public void setValueIO(ValueIO valueIO) {
		this.valueIO = valueIO;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void exportSPF(AppliedValue pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Name
		if (pojo.getName() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getName(),
					StringParameter.UTF_8));
		}

		// Set Description
		if (pojo.getDescription() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getDescription(),
					StringParameter.UTF_8));
		}

		// Set AppliedValue
		if (pojo.getAppliedValue() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else if (pojo.getAppliedValue() instanceof AbstractEntity) {
			EntityInstanceNameParameter n = new EntityInstanceNameParameter(
					appDataManager.getName(pojo.getAppliedValue()));
			sei.addParameter(n);
		} else {
			sei.addParameter(valueIO.exportSPF(pojo.getAppliedValue()));
		}

		// Set UnitBasis
		if (pojo.getUnitBasis() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new EntityInstanceNameParameter(appDataManager
					.getName(pojo.getUnitBasis())));
		}

		// Set ApplicableDate
		if (pojo.getApplicableDate() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getApplicableDate()
					.toString(), StringParameter.UTF_8));
		}

		// Set FixedUntilDate
		if (pojo.getFixedUntilDate() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getFixedUntilDate()
					.toString(), StringParameter.UTF_8));
		}

		// Set Category
		if (pojo.getCategory() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getCategory(),
					StringParameter.UTF_8));
		}

		// Set Condition
		if (pojo.getCondition() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getCondition(),
					StringParameter.UTF_8));
		}

		// Set ArithmeticOperator
		if (pojo.getArithmeticOperator() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new EnumerationParameter(pojo
					.getArithmeticOperator()));
		}

		// Set Components
		if (pojo.getComponents() == null || pojo.getComponents().isEmpty()) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (AppliedValue a : pojo.getComponents()) {
				l.add(new EntityInstanceNameParameter(appDataManager
						.getName(a)));
			}
			sei.addParameter(l);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveNotReferenceParameter(EntityInstance ei,
			AppliedValue pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Name
		Parameter name = sei.getParameter(0);
		if (name.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) name;
			pojo.setName(s.toUTF8());
		}

		// Set Description
		Parameter description = sei.getParameter(1);
		if (description.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) description;
			pojo.setDescription(s.toUTF8());
		}

		// Set ApplicableDate
		Parameter appdate = sei.getParameter(4);
		if (appdate.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) appdate;
			pojo.setApplicableDate(new Date(s.toUTF8()));
		}

		// Set FixedUntilDate
		Parameter fixdate = sei.getParameter(5);
		if (fixdate.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) fixdate;
			pojo.setFixedUntilDate(new Date(s.toUTF8()));
		}

		// Set Category
		Parameter category = sei.getParameter(6);
		if (category.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) category;
			pojo.setCategory(s.toUTF8());
		}

		// Set Condition
		Parameter condition = sei.getParameter(7);
		if (condition.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) condition;
			pojo.setCondition(s.toUTF8());
		}

		// Set ArithmeticOperator
		Parameter ar = sei.getParameter(8);
		if (ar.getNodeType() == NodeType.ENUMERATION) {
			EnumerationParameter e = (EnumerationParameter) ar;
			pojo.setArithmeticOperator(ArithmeticOperatorEnum.valueOf(e
					.toEnumString()));
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, AppliedValue pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set AppliedValue
		Parameter appvalue = sei.getParameter(2);
		if (appvalue.getNodeType() == NodeType.TYPED_PARAMETER) {
			pojo.setAppliedValue(valueIO.importSPF(appvalue));
		} else if (appvalue.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) appvalue;
			pojo.setAppliedValue(appDataManager.getObject(n.toInstanceName()));
		}

		// Set UnitBasis
		Parameter unitbasis = sei.getParameter(3);
		if (unitbasis.getNodeType() == NodeType.ENTITY_INSTANCE_NAME) {
			EntityInstanceNameParameter n = (EntityInstanceNameParameter) unitbasis;
			pojo.setUnitBasis((MeasureWithUnit) appDataManager.getObject(n
					.toInstanceName()));
		}

		// Set Components
		Parameter components = sei.getParameter(9);
		if (components.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) components;
			if (pojo.getComponents() == null) {
				pojo.setComponents(new ArrayList<AppliedValue>());
			}
			for (Parameter p : l) {
				EntityInstanceNameParameter e = (EntityInstanceNameParameter) p;
				pojo.getComponents().add(
						(AppliedValue) appDataManager.getObject(e
								.toInstanceName()));
			}
		}
	}

	@Override
	public String exportSPF(AppliedValue pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCAPPLIEDVALUE",
				appDataManager.getName(pojo));

		exportSPF(pojo, ei, appDataManager);

		return ei.toString();
	}

	@Override
	protected AppliedValue doCreateObject() {
		return new AppliedValue();
	}
}
